package com.guyaning.deyson_core.core.net.download;

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;

import com.guyaning.deyson_core.core.app.Deyson;
import com.guyaning.deyson_core.core.net.callback.IRequest;
import com.guyaning.deyson_core.core.net.callback.ISuccess;
import com.guyaning.deyson_core.core.utils.file.FileUtil;

import java.io.File;
import java.io.InputStream;

import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/4/23.
 */

public class SaveFileTask extends AsyncTask<Object,Void,File> {

    private final IRequest REQUEST;

    private final ISuccess SUCCESS;

    public SaveFileTask(IRequest REQUEST, ISuccess SUCCESS) {
        this.REQUEST = REQUEST;
        this.SUCCESS = SUCCESS;
    }


    @Override
    protected File doInBackground(Object... params) {
        String downLoadDir = (String) params[0];
        String extension   = (String) params[1];
        final ResponseBody body = (ResponseBody) params[2];
        final String name = (String) params[3];
        final InputStream stream = body.byteStream();

        if(downLoadDir == null || downLoadDir.equals("")){
           downLoadDir = "down_loads";
        }
        if(extension == null || extension.equals("")){
            extension = "";
        }
        if(name == null){
           return FileUtil.writeToDisk(stream,downLoadDir,extension.toUpperCase(),extension);
        }else {
            return  FileUtil.writeToDisk(stream,downLoadDir,name);
        }
    }


    @Override
    protected void onPostExecute(File file) {
        super.onPostExecute(file);

        if(SUCCESS!=null){
            SUCCESS.onSuccess(file.getPath());
        }

        if(REQUEST!=null){
            REQUEST.onRequestEnd();
        }

        autoInstallApk(file);
    }


    private void  autoInstallApk(File file){
        if(FileUtil.getExtension(file.getPath()).equals("apk")){
           //执行安装操作
            final  Intent install = new Intent();
            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            install.setAction(Intent.ACTION_VIEW);
            install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            Deyson.getApplicationContext().startActivity(install);
        }
    }
}
